/*   1:    */ package com.jetbrains.licenseServer.openapi.util;
/*   2:    */ 
/*   3:    */

import com.jetbrains.licenseServer.openapi.responses.AbstractResponse;
import com.jetbrains.licenseServer.openapi.responses.ResponseCode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.StringReader;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */ 
/*  19:    */ public class ResponseMarshaller
/*  20:    */ {
/*  21:    */   private static final String a = "com.jetbrains.licenseServer.openapi.responses";
/*  22:    */   private static final DocumentBuilder b;
/*  23:    */   
/*  24:    */   static
/*  25:    */   {
/*  26: 26 */     DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
/*  27: 27 */     localDocumentBuilderFactory.setIgnoringComments(true);
/*  28: 28 */     localDocumentBuilderFactory.setIgnoringElementContentWhitespace(true);
/*  29:    */     try
/*  30:    */     {
/*  31: 30 */       b = localDocumentBuilderFactory.newDocumentBuilder();
/*  32:    */     }
/*  33:    */     catch (ParserConfigurationException localParserConfigurationException)
/*  34:    */     {
/*  35: 32 */       throw new IllegalStateException(localParserConfigurationException);
/*  36:    */     }
/*  37:    */   }
/*  38:    */   
/*  39:    */   public static AbstractResponse unmarshall(String paramString)
/*  40:    */   {
/*  41:    */     try
/*  42:    */     {
/*  43: 39 */       Document localDocument = b.parse(new InputSource(new StringReader(paramString)));
/*  44:    */       
/*  45: 41 */       Element localElement = localDocument.getDocumentElement();
/*  46: 42 */       String str1 = b(localElement.getNodeName());
/*  47: 43 */       Class localClass1 = Class.forName(str1);
/*  48: 44 */       AbstractResponse localAbstractResponse = (AbstractResponse)localClass1.newInstance();
/*  49:    */       
/*  50: 46 */       PropertyDescriptor[] arrayOfPropertyDescriptor1 = Introspector.getBeanInfo(localClass1).getPropertyDescriptors();
/*  51: 47 */       for (PropertyDescriptor localPropertyDescriptor : arrayOfPropertyDescriptor1)
/*  52:    */       {
/*  53: 48 */         NodeList localNodeList = localElement.getElementsByTagName(localPropertyDescriptor.getName());
/*  54: 49 */         if (localNodeList.getLength() != 0)
/*  55:    */         {
/*  56: 50 */           Node localNode = localNodeList.item(0);
/*  57: 51 */           String str2 = unescapeXml(localNode.getTextContent());
/*  58: 52 */           Class localClass2 = localPropertyDescriptor.getPropertyType();
/*  59: 53 */           localPropertyDescriptor.getWriteMethod().invoke(localAbstractResponse, new Object[] { a(str2, localClass2) });
/*  60:    */         }
/*  61:    */       }
/*  62: 57 */       return localAbstractResponse;
/*  63:    */     }
/*  64:    */     catch (Exception localException)
/*  65:    */     {
/*  66: 59 */       throw new RuntimeException(localException);
/*  67:    */     }
/*  68:    */   }
/*  69:    */   
/*  70:    */   public static String marshall(AbstractResponse paramAbstractResponse)
/*  71:    */   {
/*  72:    */     try
/*  73:    */     {
/*  74: 65 */       StringBuilder localStringBuilder = new StringBuilder();
/*  75: 66 */       Class localClass = paramAbstractResponse.getClass();
/*  76:    */       
/*  77: 68 */       String str = a(localClass.getName());
/*  78: 69 */       localStringBuilder.append("<").append(str).append(">");
/*  79: 70 */       PropertyDescriptor[] arrayOfPropertyDescriptor1 = Introspector.getBeanInfo(localClass).getPropertyDescriptors();
/*  80: 71 */       for (PropertyDescriptor localPropertyDescriptor : arrayOfPropertyDescriptor1)
/*  81:    */       {
/*  82: 72 */         Object localObject = localPropertyDescriptor.getReadMethod().invoke(paramAbstractResponse, new Object[0]);
/*  83: 73 */         if ((localObject != null) && (!(localObject instanceof Class)))
/*  84:    */         {
/*  85: 74 */           localStringBuilder.append("<").append(localPropertyDescriptor.getName()).append(">");
/*  86: 75 */           localStringBuilder.append(c(localObject.toString()));
/*  87: 76 */           localStringBuilder.append("</").append(localPropertyDescriptor.getName()).append(">");
/*  88:    */         }
/*  89:    */       }
/*  90: 79 */       localStringBuilder.append("</").append(str).append(">");
/*  91:    */       
/*  92: 81 */       return localStringBuilder.toString();
/*  93:    */     }
/*  94:    */     catch (Exception localException)
/*  95:    */     {
/*  96: 83 */       throw new RuntimeException(localException);
/*  97:    */     }
/*  98:    */   }
/*  99:    */   
/* 100:    */   private static String a(String paramString)
/* 101:    */   {
/* 102: 88 */     return paramString.substring(paramString.lastIndexOf(".") + 1);
/* 103:    */   }
/* 104:    */   
/* 105:    */   private static String b(String paramString)
/* 106:    */   {
/* 107: 92 */     return "com.jetbrains.licenseServer.openapi.responses." + paramString;
/* 108:    */   }
/* 109:    */   
/* 110:    */   private static Object a(String paramString, Class paramClass)
/* 111:    */   {
/* 112: 96 */     if (paramClass == Long.TYPE) {
/* 113: 97 */       return Long.valueOf(Long.parseLong(paramString));
/* 114:    */     }
/* 115: 98 */     if (paramClass == Integer.TYPE) {
/* 116: 99 */       return Integer.valueOf(Integer.parseInt(paramString));
/* 117:    */     }
/* 118:100 */     if (paramClass == String.class) {
/* 119:101 */       return paramString;
/* 120:    */     }
/* 121:102 */     if (paramClass == ResponseCode.class) {
/* 122:103 */       return Enum.valueOf(ResponseCode.class, paramString);
/* 123:    */     }
/* 124:106 */     throw new RuntimeException("Unknown type in property value: " + paramClass);
/* 125:    */   }
/* 126:    */   
/* 127:    */   private static String c(String paramString)
/* 128:    */   {
/* 129:110 */     if (paramString == null) {
/* 130:111 */       return null;
/* 131:    */     }
/* 132:113 */     return Entities.XML.escape(paramString);
/* 133:    */   }
/* 134:    */   
/* 135:    */   public static String unescapeXml(String paramString)
/* 136:    */   {
/* 137:117 */     if (paramString == null) {
/* 138:118 */       return null;
/* 139:    */     }
/* 140:120 */     return Entities.XML.unescape(paramString);
/* 141:    */   }
/* 142:    */ }



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseServer.openapi.util.ResponseMarshaller

 * JD-Core Version:    0.7.0.1

 */